<template>
  <div>
    <!-- 顶部组件 -->
    <header-nav></header-nav>
     <div class="toplist">
      <div class="item" v-for="item in playList" :key="item.id">
        <router-link tag="div" :to="'/rank/ranklist?id='+item.id" class="img">
          <img :src="item.coverImgUrl" alt="">
        </router-link>
        <ul>
          <li v-for="(key,index) in item.tracks" :key="key.id" v-show="index<3">{{index+1}}.{{key.name}}-{{key.ar[0].name}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import headerNav from "../components/base/headerNav.vue";
import {getToplist,getPlaylistDetail} from "../api/rank";
export default {
 data: function () {
    return {
     playList:[]
    };
  },
  components: {
    headerNav,
  },
  //路由组件前路由守卫,提前请求数据
  beforeRouteEnter(to,from,next){
      // 在导航完成前获取数据
      // 请求第一个接口数据
      getToplist().then(data=>{
        // next();//保证请求数据执行完成后
        // 直接指定methods数据方法,调用方法
        next(vm=>vm.getPlaylistDetailFun(data.list))
      })
  },
  methods:{
    // 请求第二个接口
    getPlaylistDetailFun(list){
      for(let i = 0;i < list.length;i++){
        let id = list[i].id;
        // 第二接口
        getPlaylistDetail({id:id}).then((data)=>{
          this.playList.push(data.playlist);
        })
      }
    }
    // getToplistFun(){
    //   getToplist().then((data)=>{
    //     for(let i = 0;i <30;i++){
    //       let id = data.list[i].id;
    //       getPlaylistDetail({id:id}).then((data)=>{
    //         this.playList.push(data.playlist);
    //       })
    //     }
    //   })
    // },
  },
 created(){
    // this.getToplistFun();
   
  }
};
</script>

<style lang="less">
.toplist {
  margin-top: 5px;
  .item {
    margin: 0px 8px;
    padding: 2px 0px;
    border-bottom: 1px solid #e1e2e2;
    height: 90px;
    display: flex;
    .img {
      flex: 0 0 90px;
      margin-right: 18px;
      width: 90px;
      height: 90px;
      img {
        border-radius: 2px;
        width: 90px;
        height: 90px;
      }
    }
    ul {
      flex: 1;
      height: 90px;
      margin-top: 4px;
      li {
        height: 27px;
        line-height: 27px;
        font-size: 12px;
        color: #1b1b28;
        overflow: hidden;
      }
    }
  }
}
</style>